<%--suppress JSJQueryEfficiency --%>
<%@ page contentType="text/html;charset=UTF-8" %>
<html>
<head>
    <title>Title</title>
    <link rel="stylesheet" type="text/css"
          href="../../../js/jquery-easyui-1.9.5/themes/default/easyui.css">
    <link rel="stylesheet" type="text/css"
          href="../../../js/jquery-easyui-1.9.5/themes/icon.css">
    <link rel="stylesheet" type="text/css"
          href="../../../js/jquery-easyui-1.9.5/themes/color.css">
    <script type="text/javascript"
            src="../../../js/jquery-easyui-1.9.5/jquery.min.js"></script>
    <script type="text/javascript"
            src="../../../js/jquery-easyui-1.9.5/jquery.easyui.min.js"></script>
</head>
<body>
<table id="dept_dg" title="部门信息" class="easyui-datagrid" style="width:100%;height:100%"
       url="../dept/list"
       toolbar="#dept_toolbar"
       rownumbers="true" pagination="true" fitColumns="true" singleSelect="true">
    <thead>
    <tr>
        <th field="deptNo" style="width: 16%">部门编号</th>
        <th field="deptName" style="width: 16%">部门名称</th>
        <th field="deptDescribe" style="width: 16%">部门描述</th>
        <th field="deptManager" style="width: 16%">部门负责人</th>
        <th field="deptPhone" style="width: 16%">部门联系方式</th>
    </tr>
    </thead>
</table>

<div id="dept_toolbar">
    <label for="searchDeptNameDept">用户姓名账号:</label>
    <input type="text" name="deptName" class="easyui-textbox" id="searchDeptNameDept"/>
    <a id="searchButton" href="javascript:doDeptSearch();" class="easyui-linkbutton"
       data-options="iconCls:'icon-search'">Search</a>
    <a id="resetButton" href="javascript:doDeptReset();" class="easyui-linkbutton" data-options="iconCls:'icon-redo'">Reset</a>
    <br/>
    <a href="#" class="easyui-linkbutton" iconCls="icon-add" plain="true" onclick="newDept()">New Dept</a>
    <a href="#" class="easyui-linkbutton" iconCls="icon-edit" plain="true" onclick="editDept()">Edit Dept</a>
    <a href="#" class="easyui-linkbutton" iconCls="icon-remove" plain="true" onclick="destroyDept()">Remove Dept</a>
</div>

<%--        新增和修改对话框--%>
<div id="dept_dlg" class="easyui-dialog" style="width:400px;height:300px;padding:10px 20px"
     closed="true" buttons="#dept_dlg-buttons">
    <div class="ftitle" style="text-align: center">Dept Message</div>
    <br>
    <form id="dept_fm" method="post">
        <div class="fitem">
            <label for="deptName">部门名称&emsp;</label>
            <input id="deptName" name="deptName" class="easyui-textbox"
                   data-options="required:true,missingMessage:'deptName is not null'" prompt="deptName"
                   style="width: 250px">
        </div>
        <br>
        <div class="fitem">
            <label for="deptDescribe">部门描述&emsp;</label>
            <input id="deptDescribe" name="deptDescribe" class="easyui-textbox" prompt="deptDescribe"
                   style="width: 250px">
        </div>
        <br>
        <div class="fitem">
            <label for="deptManager">部门负责人</label>
            <input id="deptManager" name="deptManager" class="easyui-textbox" prompt="deptManager" style="width: 250px">
        </div>
        <br>
        <div class="fitem">
            <label for="deptPhone">联系方式&emsp;</label>
            <input id="deptPhone" name="deptPhone" class="easyui-textbox" prompt="deptPhone" style="width: 250px">
        </div>
    </form>
</div>

<div id="dept_dlg-buttons">
    <a href="#" class="easyui-linkbutton" iconCls="icon-ok" onclick="saveDept()">Save</a>
    <a href="#" class="easyui-linkbutton" iconCls="icon-cancel" onclick="$('#dept_dlg').dialog('close')">Cancel</a>
</div>

<script type="text/javascript">

    function doDeptSearch() {
        $("#searchDeptNameDept").textbox('setValue', $("#searchDeptNameDept").val().trim());
        //调用datagrid对象的load方法查询数据，并传递查询参数
        $("#dept_dg").datagrid("load", {
            "deptName": $("#searchDeptNameDept").val(),
        });
    }

    function doDeptReset() {
        $("#searchDeptNameDept").textbox('setValue', '');
        //清空查询参数后，重新加载数据
        $("#dept_dg").datagrid("load", {});
    }

    function newDept() {
        $('#dept_dlg').dialog('open').dialog('setTitle', 'New Dept');
        $('#dept_fm').form('clear');
        url = '../dept/add';
    }

    function editDept() {
        $('#dept_fm').form('clear');
        var row = $('#dept_dg').datagrid('getSelected');
        if (row) {
            $('#dept_dlg').dialog('open').dialog('setTitle', 'Edit Dept');
            $('#dept_fm').form('load', row);
            url = '../dept/edit?deptNo=' + row.deptNo;
        } else {
            $.messager.show({
                title: 'Error',
                msg: "请选择要修改的用户信息"
            });
        }
    }

    function destroyDept() {
        var row = $('#dept_dg').datagrid('getSelected');
        if (row) {
            $.messager.confirm('Confirm', '是否需要删除该用户？', function (r) {
                if (r) {
                    $.post('../dept/delete', {deptNo: row.deptNo}, function (result) {
                        if (result.success) {
                            $('#dept_dg').datagrid('reload');
                        } else {
                            $.messager.show({
                                title: 'Error',
                                msg: result.errorMsg
                            });
                        }
                    }, 'json');
                }
            });
        } else {
            $.messager.show({
                title: 'Error',
                msg: "请选择要删除的用户信息"
            });
        }
    }

    function saveDept() {
        $('#dept_fm').form('submit', {
            url: url,
            onSubmit: function () {
                // 提交表单前 表单验证
                return $(this).form('validate');
            },
            success: function (result) {
                // 收到服务器响应的json字符串后，调用这里的代码，回调函数当浏览器收到服务器返回的数据后才执行
                var result = eval('(' + result + ')');
                if (!result.success) {
                    $.messager.show({
                        title: 'Error',
                        msg: result.errorMsg
                    });
                } else {
                    $('#dept_dlg').dialog('close');		// 新增成功，关闭对话框
                    $('#dept_dg').datagrid('reload');	// 新增成功，刷新列表数据
                }
            }
        });
    }
</script>
</body>
</html>
